package org.NooLab.texx.properties;


import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import org.w3c.dom.Node;

import org.NooLab.texx.util.XmlStringHandling;
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.ArrUtilities;
import org.NooLab.utilities.strings.StringsUtil;



public class TexxControlProperties implements Serializable{

	private static final long serialVersionUID = -7608394827081943247L;

	TexxProperties texxProperties;
	
	String rootdir ="", projectName="";
	String settingsDir="" ,settingsFile="";

	ControlSettingsSection 	locationExcluded = null,
							locationIncluded = null,
							typesIncluded = null ;
	
	transient XmlStringHandling xMsg = new XmlStringHandling() ;
	transient StringsUtil strgutil = new StringsUtil();
	transient ArrUtilities arrutil = new ArrUtilities ();
	transient DFutils fileutil = new DFutils();
	transient PrintLog out = new PrintLog(2,true);

	// ========================================================================
	public TexxControlProperties(TexxProperties props) {
		//
		texxProperties = props;
		
	}
	// ========================================================================

	public void readSettings(String string) {
		
		
		rootdir = texxProperties.getSystemRootDir() ;
		projectName = texxProperties.getPersistenceSettings().getProjectName() ;
		
		settingsDir = DFutils.createPath( rootdir, projectName+"/model/");
		settingsFile = DFutils.createPath( settingsDir, "taskscheme.xml");
		
		if (DFutils.fileExists(settingsFile)){
			
			readControlSettingsXml(settingsFile);
		} // file ?
		else{
			// msg...
		}
		
	}

	private void readControlSettingsXml(String xFile) {
		String rawXmlStr="",str, priostr  ;
		String[] strv, strr ;
		
		Vector<Object> itemTagsContentList, nodesObjList ;
		Node node;
		
		
		
		locationExcluded = new ControlSettingsSection ();
		locationIncluded = new ControlSettingsSection ();
		typesIncluded    = new ControlSettingsSection ();
		
		
		
		
		try {
			
			rawXmlStr = fileutil.readFile2String(xFile);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (xMsg.isXML(rawXmlStr)==false){
			return;
		}
		
		xMsg.clear() ;
		xMsg.setContentRoot( "texx") ;
		
		str = xMsg.getSpecifiedInfo(rawXmlStr, "/texx", "context") ;
		

		str = xMsg.getSpecifiedInfo(rawXmlStr, "/general/operation","mode") ;
		
			
		strv = new String[2];
		strv[0] = "name";
		strv[1] = "priority";

		nodesObjList = xMsg.getNodeList(rawXmlStr, "/texx/include/type", "item") ;
					   _digestNodesObjList(nodesObjList, strv, typesIncluded);
		
		strv = new String[4];
		strv[0] = "name";
		strv[1] = "drive";
		strv[2] = "basefolder";
		strv[3] = "priority";

		nodesObjList = xMsg.getNodeList(rawXmlStr, "/texx/include/location", "item") ;
					   _digestNodesObjList(nodesObjList, strv, locationIncluded);  
		
		
		nodesObjList = xMsg.getNodeList(rawXmlStr, "/texx/exclude/location", "item") ;
					   _digestNodesObjList(nodesObjList, strv, locationExcluded);
		
		
		priostr = xMsg.getSpecifiedInfo(rawXmlStr, "/exclude/extension/items","priority") ; 			
		str = xMsg.getSpecifiedInfo(rawXmlStr, "/exclude/extension/items","list") ; 			

		itemTagsContentList = xMsg.getItemsList(rawXmlStr, "/texx/exclude/extension", "item", "name") ; 			

		/*
		      <extension>
		         <items list=".js;bak;tmp;.jpg;jpeg;tiff;png;bmp;" />
		         <item name="css;.java;" /> 
		      </extension>
		*/
			if (itemTagsContentList!=null){
			
				for (int i=0;i<itemTagsContentList.size();i++){
					str = str+ ";"+(String)itemTagsContentList.get(i);
					
				}// i->
			} // ?
			if (str.length()>0){
				strr = str.split(";");
			}
		
		// --------------------------------------------- 
		itemTagsContentList = xMsg.getItemsList(rawXmlStr, "/texx/exclude/type", "item", "name") ; // [image, video, sound, program]

		if (itemTagsContentList!=null){
			for (int i=0;i<itemTagsContentList.size();i++){
				str = (String)itemTagsContentList.get(i);
				// "[image, video, sound, program]"
				strr = str.split(";");
			}// i->
		}
		str=" ";
		
		
	}
	
	
	
	private void _digestNodesObjList(Vector<Object> nodesObjList, String[] strv ,ControlSettingsSection _section){
		
		String rawXmlStr="",str ;
		String[] strr ;
		
		Node node;

		String[] cats = new String[strv.length] ;
		System.arraycopy(strv, 0, cats, 0, strv.length);
		_section.attributes = new ArrayList<String>(Arrays.asList(cats));
		_section.attrValues = new ArrayList<String>();
		
		if (nodesObjList != null) {

			
			strr = new String[ strv.length];

			for (int i = 0; i < nodesObjList.size(); i++) {
				node = (Node) nodesObjList.get(i);
				strr = xMsg.getAttrValuesForNode(node, "item", strv);
				
				for (int s=0;s<strr.length;s++){
					_section.attrValues.add(strr[s]) ;
				}
			}

		// create a object from the array of results

		// put it to a list
	}


	}

	public ControlSettingsSection getLocationExcluded() {
		return locationExcluded;
	}

	public ControlSettingsSection getLocationIncluded() {
		return locationIncluded;
	}

	public ControlSettingsSection getTypesIncluded() {
		return typesIncluded;
	}
	
}
